﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using USERS.CYT369.COM.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;

namespace USERS.CYT369.COM.Controllers
{
    public partial class NewsController : AuthorizeController
    {
        #region Article
        [HttpGet]
        [HttpPost]
        public IActionResult ArticleIndex()
        {
            var status = Request.Query.GetInteger("status");

            return GetDocuments("Articles/ArticleIndex", status);
        }

        public IActionResult AuditPassedArticles()
        {
            return GetDocuments("Articles/AuditPassedArticles", 1);
        }

        public IActionResult AuditFailedArticles()
        {
            return GetDocuments("Articles/AuditFailedArticles", 2);
        }

        public IActionResult ArticleDraft()
        {
            return GetDocuments("Articles/ArticleDraft", 0);
        }

        public IActionResult ArticleDel()
        {
            return GetDocuments("Articles/ArticleDel", null, true);
        }

        private IActionResult GetDocuments(string view, int? status, bool deleted = false)
        {
            int? year = null;
            int? month = null;
            int? documentTypeId = null;
            string title = null;

            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 10);
            int count = 0;

            if (Request.HasFormContentType)
            {
                year = Request.Form.GetInteger("docYears");
                month = Request.Form.GetInteger("docMonths");
                documentTypeId = Request.Form.GetInteger("documentType");
                title = Request.Form.GetString("search-key");
            }

            var documents = USERS.CYT369.COM.DAL.DocumentService.Instance.GetDocuments(page, rows, ref count, this.GetUserId(), status, title, year, month, documentTypeId);
            ViewBag.Stastics = USERS.CYT369.COM.DAL.DocumentService.Instance.GetDocumentStastics(this.GetUserId());
            ViewBag.UserID = this.GetUserId();
            ViewBag.SearchYear = year;
            ViewBag.SearchMonth = month;
            ViewBag.SearchDocumentTypeID = documentTypeId;
            ViewBag.SearchTitle = title;

            ViewBag.PageNumber = page;
            ViewBag.PageRows = rows;
            ViewBag.TotalCount = count;

            return View(view, documents);
        }

        public IActionResult ArticleCategory()
        {
            return View("Articles/ArticleCategory");
        }

        public IActionResult EditArticle(string id)
        {
            int userId = this.GetUserId();
            USERS.CYT369.COM.MODELS.Articles.Document document = null;
            if (!string.IsNullOrEmpty(id))
            {
                var argsJson = Base64.Base64ToJSON(id);
                long? docId = argsJson.GetInteger("NID");
                if (docId.HasValue)
                {
                    document = USERS.CYT369.COM.DAL.DocumentService.Instance.GetDocumentByID_IncludeImg(docId.Value, userId);
                }
            }

            ViewBag.UserID = userId;

            return View("Articles/EditArticle", document);
        }

        #endregion

    }
}
